<?php

namespace app\task\model;

use app\common\model\Rechargr as RechargrModel;

use app\common\model\Setting as SettingModel;
use app\common\model\User as UserModel;
use app\common\model\UserScore as UserScoreModel;
use think\Db;

/**
 * 订单模型
 */
class Order extends OrderModel
{
    /**
     * 待支付订单详情
     */
    public function payDetail($order_no)
    {
        return self::get(['order_no' => $order_no]);
    }

    /**
     * 更新付款状态
     */
    public function updatePayStatus($transaction_id, $order)
    {
		// 开启事务
        Db::startTrans();
		
			// 更新订单状态
			$this->save([
				'status' => 1,
				'pay_time' => time(),
				'transaction_id' => $transaction_id,
			]);
			
		$user = UserModel::detail(['user_id' => $order['user_id']]);//获取用户详情
		//获取积分配置
		$s_set = SettingModel::getItem('score');
		if(!empty($s_set)){
			if($s_set['is_open']==1){
				//增加用户积分
				$user->score = ['inc', intval($order['money'])*$s_set['gift_multiple']]; //去尾取整增加积分
				
				//增加积分变动记录
				$score = new UserScoreModel;
				$score->values = intval($order['money'])*$s_set['gift_multiple']; //积分值
				$score->reason = '会员充值';
				$score->user_id = $order['user_id'];//用户
				$score->wxapp_id = $order['wxapp_id'];//商户
				$score->save();
			}
		}
		$user->pay = ['inc', $order['money']];//增加消费金额
		$wallet = $order['money']+$order['gift_money'];
		$user->wallet = ['inc', $wallet];//增加消费金额
		$user->save();
		
		Db::commit();
        return true;
    }

}
